﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Power.Models;
using Power.View.Dialog;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;

namespace Power.ViewModel.Dialog
{
    public class AuthorityManagementViewModel:ViewModelBase
    {
        private AuthorityManagement model;
        public AuthorityManagementViewModel(AuthorityManagement model)
        {
            this.model = model;
            this.model.page.PageUpdated += Page_PageUpdated;
            Close = new RelayCommand(OnClose);
            BatchAgree = new RelayCommand(OnBatchAgree);
            BatchReject = new RelayCommand(OnBatchReject);
            CheckBoxClick = new RelayCommand<int>(OnCheckBoxClick);
            Agree = new RelayCommand<int>(OnAgree);
            Reject = new RelayCommand<int>(OnReject);
            Data = new ObservableCollection<VerifyManaEntity>()
            {
                new VerifyManaEntity(){ 
                    ID = 1,
                    ProjectName = "项目1",
                    Role="角色1",
                    UserName="用户1"
                },
                new VerifyManaEntity(){
                    ID = 2,
                    ProjectName = "项目2",
                    Role="角色2",
                    UserName="用户2"
                }
            };
            ProSelectList = new List<SelectListItem>()
            {
                new SelectListItem(){SelectItem="项目1",SelectID="1" },
                new SelectListItem(){SelectItem="项目2",SelectID="2" }
            };
        }

        private void Page_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs<int> e)
        {
            var currentPage = e.Info;
        }

        /// <summary>
        /// 拒绝
        /// </summary>
        private void OnReject(int id)
        {
            var item = Data.Where(p => p.ID == id).FirstOrDefault();
            if (item != null)
            {
                Data.Remove(item);
            }
        }

        /// <summary>
        /// 同意
        /// </summary>
        private void OnAgree(int id)
        {
            var item = Data.Where(p => p.ID == id).FirstOrDefault();
            if (item != null)
            {
                Data.Remove(item);
            }
        }

        /// <summary>
        /// 批量拒绝
        /// </summary>
        private void OnBatchReject()
        {
            
            var list = Data.Where(p => p.IsSelectedFlag == true).ToList();
            if (list.Count>0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    Data.Remove(list[i]);
                }
                
            }
        }

        /// <summary>
        /// 批量同意
        /// </summary>
        private void OnBatchAgree()
        {
            var list = Data.Where(p => p.IsSelectedFlag == true).ToList();
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    Data.Remove(list[i]);
                }
            }
        }

        private void OnCheckBoxClick(int obj)
        {
            var item = Data.Where(p => p.ID == obj).FirstOrDefault();
            if (item != null)
            {
                item.IsSelectedFlag = !item.IsSelectedFlag;
            }
        }


        private void OnClose()
        {
            this.model.DialogResult = false;
            this.model.Close();
        }

        #region 
        public RelayCommand Close { get; private set; }
        public RelayCommand BatchAgree { get; private set; }
        public RelayCommand BatchReject { get; private set; }
        public RelayCommand<int> Agree { get; private set; }
        public RelayCommand<int> Reject { get; private set; }
        public RelayCommand<int> CheckBoxClick { get; private set; }
        #endregion

        #region 数据绑定
        private ObservableCollection<VerifyManaEntity> data;
        public ObservableCollection<VerifyManaEntity> Data
        {
            get
{
                return data;
            }
            set
            {
                data = value;
                RaisePropertyChanged();
            }
        }

        //管道
        private List<SelectListItem> proSelectList;
        public List<SelectListItem> ProSelectList
        {
            get => proSelectList;
            set
            {
                proSelectList = value;
                RaisePropertyChanged();
            }
        }

        private VerifyManaEntity selectedItem;


        private bool checkBoxIsChecked;

        public bool CheckBoxIsChecked
        {
            get
            {
                return checkBoxIsChecked;
            }
            set
            {
                checkBoxIsChecked = value;
                ChangedSelecgedFlags();
                RaisePropertyChanged();
                RaisePropertyChanged("Data");
            }
        }
        private void ChangedSelecgedFlags()
        {
            if (CheckBoxIsChecked == false)
            {
                foreach (var e in Data)
                    e.IsSelectedFlag = false;
            }
            else
            {
                foreach (var e in Data)
                    e.IsSelectedFlag = true;
            }
        }

        public VerifyManaEntity SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                selectedItem = value;
                RaisePropertyChanged();
            }
        }
        public int PageIndex { get; set; } = 1;
        #endregion
    }
}
